<?php 

















//文章目录：https://www.npc.ink/4720.html
function article_index($content) {

   $matches = array();
   $ul_li = '';

   $r = "/<h3>([^<]+)<\/h3>/im";

   if(is_singular() && preg_match_all($r, $content, $matches)) {
      foreach($matches[1] as $num => $title) {
         $title = trim(strip_tags($title));
         $content = str_replace($matches[0][$num], '<h3 id="title-'.$num.'">'.$title.'</h3>', $content);
         $ul_li .= '<li><a href="#title-'.$num.'" title="'.$title.'">'.$title."</a></li>\n";
      }

      $content = "\n<div id=\"article-index\">
<strong>文章目录</strong>
<ol id=\"index-ul\">\n" . $ul_li . "</ol>
</div>\n" . $content;
   }

   return $content;
}

add_filter( 'the_content', 'article_index' );



?>

<?php 









//默认支持记录评论者的Cookie：https://www.npc.ink/4666.html
add_action('set_comment_cookies','coffin_set_cookies',10,3);
 function coffin_set_cookies( $comment, $user, $cookies_consent){
 $cookies_consent = true;
 wp_set_comment_cookies($comment, $user, $cookies_consent);
 }




//评论表单：https://www.npc.ink/4655.html
function alter_comment_form_fields($fields){
    $fields['author'] = '<p class="comment-form-author"><label for="author"> </label> <span class="required"></span><input type="text" placeholder="&nbsp;&nbsp;称呼" aria-required="true" size="30" value="' . esc_attr( $commenter['comment_author'] ) . '" name="author" id="author"></p>';
	
    $fields['email'] = '<p class="comment-form-email"><label for="email"></label> <span class="required"></span><input type="text" placeholder="&nbsp;&nbsp;电子邮件" aria-required="true" size="30" value="' . esc_attr( $commenter['comment_author_email'] ) . '" name="email" id="email"></p>';
	
    $fields['url'] =  '<p class="comment-form-url"><label for="url"></label><input type="text" placeholder="&nbsp;&nbsp;网站" size="30" value="'.$comment_author_url.'" name="url" id="url"></p>';
    return $fields;
}

add_filter('comment_form_default_fields','alter_comment_form_fields');





/*评论单：https://codex.wordpress.org/Function_Reference/wp_list_comments*/
function mirages_comment($comment, $args, $depth) {
    if ( 'div' === $args['style'] ) {
        $tag       = 'div';
        $add_below = 'comment';
    } else {
        $tag       = 'li';
        $add_below = 'div-comment';
    }?>
    <<?php echo $tag; ?> <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ); ?> id="comment-<?php comment_ID() ?>"><?php 
    if ( 'div' != $args['style'] ) { ?>
        <div id="div-comment-<?php comment_ID() ?>" class="comment-body"><?php
    } ?>
        <div class="comment-author vcard"><?php 
            if ( $args['avatar_size'] != 0 ) {
                echo get_avatar( $comment, $args['avatar_size'] ); 
            } 
	//XX说
            printf( __( '<cite class="fn">%s</cite> <span class="says"></span>' ), get_comment_author_link() ); ?>
        </div>
			
			
			 <div class="comment-meta commentmetadata">
            <span><a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ); ?>">
				<?php
                /* translators: 1: date, 2: time */
                printf( 
                    __('%1$s at %2$s'), 
                    get_comment_date(),  
                    get_comment_time() 
                ); ?>
            </a></span>
				 <div class="comment-meta commentmetaoption">
			<?php 
            edit_comment_link( '<i class="fas fa-eraser"></i>', '' ); ?><!--编辑-->
			
			<?php 
                comment_reply_link( 
                    array_merge( 
                        $args, 
                        array( 
							'reply_text' => '<i class="fas fa-paper-plane" alt="回复"></i>',//回复
                            'add_below' => $add_below, 
                            'depth'     => $depth, 
                            'max_depth' => $args['max_depth'] 
                        ) 
                    ) 
                ); ?>
				 </div>
      
        </div>
			
			<?php 
        if ( $comment->comment_approved == '0' ) { ?>
            <em class="comment-awaiting-moderation"><?php _e( '您的评论正在等待审核.233' ); ?></em><br/><?php 
        } ?>
       

        <?php comment_text(); ?>

       
			<?php 
    if ( 'div' != $args['style'] ) : ?>
        </div>
		<?php 
    endif;
}









//禁用所有文章类型的修订版本
//可加强博文安全性，略微降低加载速度，不建议使用。
//
//add_filter( 'wp_revisions_to_keep', 'specs_wp_revisions_to_keep', 10, 2 );
//function specs_wp_revisions_to_keep( $num, $post ) {
//return 0;
//}


//禁止 WordPress5.0 使用 Gutenberg 块编辑器：https://www.npc.ink/4517.html
add_filter('use_block_editor_for_post', '__return_false');
remove_action( 'wp_enqueue_scripts', 'wp_common_block_scripts_and_styles' );









//检测主题更新
require get_template_directory() . '/inc/theme-update-checker.php';
$example_update_checker = new ThemeUpdateChecker(
    'Mirages',
    'https://www.npc.ink/theme-updates/mirages.json '
);

//底部时间：https://www.npc.ink/4625.html
function comicpress_copyright() {
global $wpdb;
$copyright_dates = $wpdb->get_results("
SELECT
YEAR(min(post_date_gmt)) AS firstdate,
YEAR(max(post_date_gmt)) AS lastdate
FROM
$wpdb->posts
WHERE
post_status = 'publish'
");
$output = '';
if($copyright_dates) {
$copyright = "&copy; " . $copyright_dates[0]->firstdate;
if($copyright_dates[0]->firstdate != $copyright_dates[0]->lastdate) {
$copyright .= '-' . $copyright_dates[0]->lastdate;
}
$output = $copyright;
}
return $output;
}
//评论嵌套：https://www.npc.ink/4625.html
// enable threaded comments
function enable_threaded_comments(){
if (!is_admin()) {
if (is_singular() AND comments_open() AND (get_option('thread_comments') == 1))
wp_enqueue_script('comment-reply');
}
}
add_action('get_header', 'enable_threaded_comments');



/* 访问计数 */
function record_visitors()
{
	if (is_singular())
	{
	  global $post;
	  $post_ID = $post->ID;
	  if($post_ID)
	  {
		  $post_views = (int)get_post_meta($post_ID, 'views', true);
		  if(!update_post_meta($post_ID, 'views', ($post_views+1)))
		  {
			add_post_meta($post_ID, 'views', 1, true);
		  }
	  }
	}
}
add_action('wp_head', 'record_visitors');
 
/// 函数名称：post_views
/// 函数作用：取得文章的阅读次数
function post_views($before = '(点击 ', $after = ' 次)', $echo = 1)
{
  global $post;
  $post_ID = $post->ID;
  $views = (int)get_post_meta($post_ID, 'views', true);
  if ($echo) echo $before, number_format($views), $after;
  else return $views;
}



//给外部链接加上跳转  
function git_go_url($content){  
    preg_match_all('/<a(.*?)href="(.*?)"(.*?)>/',$content,$matches);  
    if($matches && !is_page('about')){  
        foreach($matches[2] as $val){  
            if(strpos($val,'://')!==false && strpos($val,home_url())===false && !preg_match('/\.(jpg|jepg|png|ico|bmp|gif|tiff)/i',$val)){  
                if(git_get_option('git_pagehtml_b')) {  
                    $content=str_replace("href=\"$val\"", "href=\"".home_url()."/go.html/?url=$val\" ",$content);  
                }else{  
                    $content=str_replace("href=\"$val\"", "href=\"".home_url()."/go/?url=$val\" ",$content);  
                }  
            }  
        }  
    }  
    return $content;  
}  




?>
	   
<?php 
//以下为各种加载项目，有利于对主题的理解
//加载项目
//加载评论样式
function mirages_comments_css(){
	wp_register_style('comments',get_template_directory_uri().'/css/comments.css');
	wp_enqueue_style('comments', get_template_directory_uri().'/css/comments.css');
}
add_action('wp_enqueue_scripts','mirages_comments_css');
		
		
	
		
		

//https://www.npc.ink/4521.html
//测试设置
require_once('test.php');

//设置面板
require get_template_directory() . '/inc/settings-panel.php';

//常用函数
require get_template_directory() . '/inc/commonly-used-functions.php';

//移除顶部工具栏：https://www.npc.ink/4574.html
//show_admin_bar(false);

//加载各种页面
require get_template_directory() . '/inc/pages.php';
//加载自定义邮件页面：https://liuyanzhao.com/4417.html
require get_template_directory() . '/inc/notify.php';


//加入SEO：https://www.npc.ink/4596.html
require_once get_stylesheet_directory() . '/seo/admin-meta.php'; 
require_once get_stylesheet_directory() . '/seo/seo-options.php';
function qiwei_seo_title() { get_template_part('/seo/seo'); }
	   
?>